#include "game.hpp"

using namespace std;

game::game()
{
    SWidth = 640;
    SHeight = 480;
    SBpp = 32;
    quit = false;
    STitle = "Project Lyons 0.0.3";

    init(screen, SWidth, SHeight, STitle);
    screen = SDL_SetVideoMode( SWidth, SHeight, 32, SDL_SWSURFACE );

    p = player(40,40);
    //m = map();
    m.import("map1");
    m.importTextures();
    m.mapOut();

    textColor = {0, 0, 0};
    TTF_Init();
    font = TTF_OpenFont( "arial.ttf", 28 );
}

game::game(int x, int y)
{
    SWidth = x;
    SHeight = y;
    SBpp = 32;
    STitle = "Project Lyons 0.0.3";

    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
    }
    screen = SDL_SetVideoMode( SWidth, SHeight, SBpp, SDL_SWSURFACE );


    SDL_WM_SetCaption( STitle.c_str(), NULL );
}
void game::run()
{
    while(quit == false)
    {
        if(SDL_PollEvent( &event ))
        {
            if(event.type == SDL_QUIT)
            {
                quit = true;
            }
        }
        Uint8 *keystates = SDL_GetKeyState( NULL );
        p.move(keystates[ SDLK_UP ] || keystates[ SDLK_w ], keystates[ SDLK_DOWN ] || keystates[ SDLK_s ], keystates[ SDLK_LEFT ] || keystates[ SDLK_a ], keystates[ SDLK_RIGHT] || keystates[ SDLK_d ] , m.highestPoint(p.x,p.y), m.lowestPoint(p.x, p.y+39), m.rightMostPoint(p.x, p.y));
        cDetect();
        if(keystates[ SDLK_ESCAPE ])
            quit = true;
        draw();
    }
}
void game::cDetect()
{
    if(p.y + 41 < m.highestPoint(p.x,p.y))
    {
        p.setFalling(true);
    }
    else
        p.setFalling(false);
}

void game::draw()
{
    m.draw(screen);

    stringstream ss;
    ss << m.highestPoint(p.x, p.y);
    string test;
    ss >> test;
    apply_surface(50,50, TTF_RenderText_Solid( font, test.c_str(), textColor ), screen);//*/

    stringstream s2;
    s2 << m.lowestPoint(p.x, p.y);
    s2 >> test;
    apply_surface(50,100, TTF_RenderText_Solid( font, test.c_str(), textColor ), screen);

    stringstream s3;
    s3 << m.rightMostPoint(p.x, p.y);
    s3 >> test;
    apply_surface(50,150, TTF_RenderText_Solid( font, test.c_str(), textColor ), screen);

    p.draw(screen);
    SDL_Flip(screen);
}
